#include <QDate>
#include <QDebug>
#include <QStandardItemModel>


#include "inspectionswidget.h"
#include "ui_inspectionswidget.h"
#include "datedelegate.h"
#include "floatdelegate.h"
#include "promenidelegate.h"
#include "defs.h"
#include "comboboxdelegate.h"
//#include "positiondelegate.h"
#include "manager.h"

InspectionsWidget::InspectionsWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::InspectionsWidget)
{
    ui->setupUi(this);
}

InspectionsWidget::~InspectionsWidget()
{
    delete ui;
}

void InspectionsWidget::setup()
{
    setupModel();
    setupView();

    connect(ui->btnAdd, SIGNAL(clicked()),
            App->mainWindow(), SLOT(markAsModified()));
    connect(ui->btnDel, SIGNAL(clicked()),
            App->mainWindow(), SLOT(markAsModified()));

    ui->btnDel->setEnabled(false);
}

void InspectionsWidget::clearAll()
{
    ui->btnDel->setEnabled(false);
}

void InspectionsWidget::setupModel()
{
    if (App->pacient()->inspections() == NULL)
    {
        LOG(objectName(), "App->pacient()->inspections() == NULL");
    } else {
        ui->tblInspections->setModel( App->pacient()->inspections() );
    }
}

void InspectionsWidget::setupView()
{
    QTableView *tw = ui->tblInspections;
    tw->horizontalHeader()->setStretchLastSection(true);
    tw->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);

    tw->setItemDelegateForColumn(IC_Date, new DateDelegate(this));
    fd = new FloatDelegate(this);
    tw->setItemDelegateForColumn(IC_Age, fd);



    tw->setItemDelegateForColumn( IC_Limb, new ComboBoxDelegate(LimbStrings, this) );

    for (int i=IC_UStegno; i<IC_Prognoz; ++i)
    {
        if (i!=IC_Promeni)
            tw->setItemDelegateForColumn(i, fd);
    }

    tw->setItemDelegateForColumn(IC_Promeni, new PromeniDelegate(this));

    tw->setItemDelegateForColumn(IC_Prognoz, new ComboBoxDelegate(PrognozStrings, this));
    ui->btnDel->setEnabled(false);

}

void InspectionsWidget::addLine(const QVariantList &vl)
{
    const int newRowCount = App->pacient()->inspections()->rowCount();
    App->pacient()->addInspection( vl );
    ui->tblInspections->selectRow(newRowCount);
    ui->btnDel->setEnabled(true);
}

void InspectionsWidget::delLine(const int rowNumber)
{
    if (rowNumber>=0 && rowNumber < App->pacient()->inspections()->rowCount())
    {
        App->pacient()->inspections()->removeRow(rowNumber);
        if (rowNumber>0)
            ui->tblInspections->selectRow(rowNumber - 1);
    }
}

void InspectionsWidget::setEnabledBtnDel()
{
    ui->btnDel->setEnabled(true);
}

void InspectionsWidget::on_btnAdd_clicked()
{
    QDate curDate(QDate::currentDate());
    QVariantList vl;
    for (int i=0; i<InspectionColCount; ++i)
        vl.append(QString(""));
    vl[IC_Date] = curDate;
    vl[IC_Age] = App->pacient()->ageAtDate(curDate);
    vl[IC_Limb] = 0;
    vl[IC_Prognoz] = 0;

    addLine( vl );
    ui->btnDel->setEnabled(true);
}

void InspectionsWidget::on_btnDel_clicked()
{
    const int curRowNum = ui->tblInspections->currentIndex().row();
    delLine(curRowNum);
    if (App->pacient()->inspections()->rowCount() == 0)
        ui->btnDel->setEnabled(false);
}
